package com.pichub.psdmaster;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 功能描述：连接数据库-mysql
 *
 * @author :FangHewei
 * @Date :Jul 18, 2008
 * @Time :3:28:59 PM
 * @version :1.0
 */
public class ConnectMainDB {

	private static String access_key;
	private static String secret_key;
	private static String MainURL;
	private static Logger logger = Logger.getLogger("appender1");
	private static ComboPooledDataSource cpds = new ComboPooledDataSource();
	static {
		try {
			InputStream in = ConnectMainDB.class.getClassLoader()
					.getResourceAsStream("dbinfo.properties");
			Properties props = new Properties();
			props.load(in);
			MainURL = props.getProperty("MainURL");
			access_key =  props.getProperty("access_key");
			secret_key =  props.getProperty("secret_key");
//			MainURL = "jdbc:mysql://115.28.43.137:3306/PicHub";
//			access_key =  "root";
//			secret_key =  "zhangruhong";
			// 连接主库
			cpds.setJdbcUrl(MainURL);
			cpds.setUser(access_key);
			cpds.setPassword(secret_key);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	public static Connection getMainConnection() {
		Connection conn = null;
		try {
			conn = cpds.getConnection();
			return conn;
		} catch (SQLException e) {
			logger.error("主数据库连接失败",e);
			return null;
		}
	}

}
